home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4159 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.3 KB

  1. Path: news.th-darmstadt.de!news!enno
  2. From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: novice question on copy constru
  5. Date: 28 Jan 1996 15:15:59 GMT
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Distribution: world
  8. Message-ID: <ENNO.96Jan28161559@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. References: <4efpie$jsq@news.ust.hk>
  10. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  11. In-reply-to: ee_ckmaa@uxmail.ust.hk's message of 28 Jan 1996 12:18:22 GMT
  12.  
  13. In article <4efpie$jsq@news.ust.hk> ee_ckmaa@uxmail.ust.hk (Chan Ka Ming) writes:
  14.  
  15.    I don't understand why one should create a copy constructor. Doesn't the
  16.    computer will do the job for you when pass arguments by value? Thanks
  17.  
  18. If you don't define a copy-constructor the compile will provide the
  19. 'default copy-constructor', which performs a 'memberwise' copy, ie.
  20. the appropriate copy-constructors of the base-classes and non-static
  21. data-members are called. Unfortunately this behavior is not suitable
  22. for all situations. For example in the following class memberwise
  23. copying is not the right solution
  24.  
  25.         struct A {
  26.           int* i_;
  27.           A() : i_(new int(4711)) {}
  28.           ~A() { delete i_; }
  29.             };
  30.  
  31. because only the pointer-value and not the related data is copied.
  32. Thus in the subsequent snippet
  33.  
  34.         A a1;
  35.         A a2(a1);
  36.  
  37. 'a1' and 'a2' share the same data, which will be therefore released
  38. two times. This problem can easily removed by adding an appropriate
  39. copy-constructor (cc). Similar problems could occur when you assign
  40. an existing object to another. So an additional assignment-operator
  41. (as) is also a good idea. The modified class looks like:
  42.  
  43.         struct A {
  44.           int* i_;
  45.           A() : i_(new int(4711)) {}
  46.           A(const A& a) : i_(new int(*a.i_)) {}     // (cc)
  47.           A& operator = (const A& a) {             // (as)
  48.             if (this!=&a) { delete i_; i_=new int(*a.i_); }
  49.             return *this;
  50.                   }
  51.           ~A() { delete i_; }
  52.             };
  53.  
  54. After this change 'A' has 'proper copy-semantics'. You can use it in
  55. other classes and don't have to care about how objects of 'A' are
  56. copied -- the compiler creates the necessary code for calling the (cc)
  57. or (as). For example the class 'B' defined as
  58.  
  59.         struct B { A a_; };
  60.  
  61. has also proper copy-semantics because the default-versions of (cc)
  62. and (as) do memberwise _not_ bitwise copy of the single data-member.
  63.  
  64.     Enno
  65.